from wsgiref import headers

# import requests


# gear_url = 'https://g0.gph.netease.com/ngsocial/community/stzb/cfg/gear_id.json?gameid=g10'
#
# gear_response = requests.get(gear_url)
# gear_datas = gear_response.json()
# for gear_data in gear_datas:
#     print(gear_data)
#     gear_id = gear_data.get("id")
#     gear_icon_url = f'https://g0.gph.netease.com/ngsocial/community/stzb/cn/gears/gear_icon/gear_icon_{gear_id}.jpg?gameid=g10'
#     gear_icon_response = requests.get(gear_icon_url)
#     print(gear_icon_response.content)
#
#     break



# skill_url = 'https://g0.gph.netease.com/ngsocial/community/stzb/cfg/skill_extra.json?gameid=g10'
# skill_response = requests.get(skill_url)
# skill_datas = skill_response.json()
# for skill_data in skill_datas:
#     print(skill_data)
#     break




import requests
import pymongo
import os
import shutil

client = pymongo.MongoClient('localhost', 27017)
db = client.get_database('率土之滨')
collection = db.get_collection('武将库')



hero_url = 'https://g0.gph.netease.com/ngsocial/community/stzb/cfg/hero_extra.json?gameid=g10'
hero_response = requests.get(hero_url)
hero_datas = hero_response.json()

# 插入列表
collection.insert_many(hero_datas)
client.close()

# 保存图片
directory_path = 'static/率土之滨/武将库'
if not os.path.exists(directory_path):
    os.makedirs(directory_path)
else:
    shutil.rmtree(directory_path)
    os.makedirs(directory_path)

for hero_data in hero_datas:
    # print(hero_data)
    hero_id = hero_data.get("id")
    hero_name = hero_data.get("name")
    hero_icon_url = f'https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_{hero_id}.jpg?gameid=g10'
    hero_icon_response = requests.get(hero_icon_url)
    # print(hero_icon_response.content)

    with open(f'{directory_path}/{hero_name}.jpg', 'wb') as f:
        f.write(hero_icon_response.content)

    break




# html->js->ajax(json)
# js
# 1. getJson
# 2. 对应元素的操作事件
# 2.1 搜元素属性或者数据
# 2.2 事件侦听器
# $("#role_list").html(str)   json数据填充html页面

